//读者管理模块
import java.sql.*;
import java.util.Scanner;

public class ReaderManager {
    private Scanner scanner = new Scanner(System.in);

    public void menu() {
        while (true) {
            System.out.println("\n--- 读者管理 ---");
            System.out.println("1. 添加读者");
            System.out.println("2. 删除读者");
            System.out.println("3. 查看所有读者");
            System.out.println("4. 返回主菜单");
            int choice = getIntInput("请选择操作：");

            switch (choice) {
                case 1:
                    addReader();
                    break;
                case 2:
                    deleteReader();
                    break;
                case 3:
                    listReaders();
                    break;
                case 4:
                    return;
                default:
                    System.out.println("无效选择，请重试。");
            }
        }
    }

    private void addReader() {
        String name = getStringInput("请输入读者姓名：");
        String studentId = getStringInput("请输入学号：");

        String checkSql = "SELECT id FROM readers WHERE student_id = ?";
        String insertSql = "INSERT INTO readers (name, student_id) VALUES (?, ?)";

        try (Connection conn = DBHelper.getConnection()) {
            // 验证学号是否已存在
            try (PreparedStatement checkStmt = conn.prepareStatement(checkSql)) {
                checkStmt.setString(1, studentId);
                ResultSet rs = checkStmt.executeQuery();
                if (rs.next()) {
                    System.out.println("该学号已存在，不能重复添加。");
                    return;
                }
            }

            // 添加读者
            try (PreparedStatement insertStmt = conn.prepareStatement(insertSql)) {
                insertStmt.setString(1, name);
                insertStmt.setString(2, studentId);
                insertStmt.executeUpdate();
                System.out.println("读者添加成功。");
            }

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("添加读者失败： " + e.getMessage());
        }
    }

    private void deleteReader() {
        int readerId = getIntInput("请输入读者ID：");

        String checkSql = "SELECT id FROM readers WHERE id = ?";
        String deleteSql = "DELETE FROM readers WHERE id = ?";

        try (Connection conn = DBHelper.getConnection()) {
            // 验证读者是否存在
            try (PreparedStatement checkStmt = conn.prepareStatement(checkSql)) {
                checkStmt.setInt(1, readerId);
                ResultSet rs = checkStmt.executeQuery();
                if (!rs.next()) {
                    System.out.println("读者ID不存在。");
                    return;
                }
            }

            // 删除读者
            try (PreparedStatement deleteStmt = conn.prepareStatement(deleteSql)) {
                deleteStmt.setInt(1, readerId);
                deleteStmt.executeUpdate();
                System.out.println("读者删除成功。");
            }

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("删除读者失败： " + e.getMessage());
        }
    }

    private void listReaders() {
        String sql = "SELECT id, name, student_id FROM readers";

        try (Connection conn = DBHelper.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            System.out.println("\n读者ID\t姓名\t学号");
            while (rs.next()) {
                System.out.printf("%d\t%s\t%s\n",
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("student_id"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询读者失败： " + e.getMessage());
        }
    }

    private int getIntInput(String prompt) {
        while (true) {
            System.out.print(prompt);
            try {
                return Integer.parseInt(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.println("请输入合法数字！");
            }
        }
    }

    private String getStringInput(String prompt) {
        System.out.print(prompt);
        return scanner.nextLine().trim();
    }
}
